package cn.id68.www.http.request;

import java.io.UnsupportedEncodingException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import cn.id68.www.http.builder.PostFormBuilder;
import cn.id68.www.http.callback.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/3/21.
 */

public class PostFormRequest extends OkHttpRequest {

    private List<PostFormBuilder.FileInput> files;

    public PostFormRequest(String url, Object tag, Map<String, String> params, Map<String, String> headers, int id, List<PostFormBuilder.FileInput> files) {
        super(url, tag, params, headers, id);
        this.files = files;
    }

    @Override
    protected RequestBody buildRequestBody() {
        if (files == null || files.isEmpty()){
            FormBody.Builder builder = new FormBody.Builder();
            addParams(builder);
            FormBody formBody = builder.build();
            return formBody;
        }else {
            MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
            addParams(builder);
            for (int i=0;i < files.size();i++){
                PostFormBuilder.FileInput fileInput = files.get(i);
                RequestBody fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileInput.filename)),fileInput.file);
                builder.addFormDataPart(fileInput.key,fileInput.filename,fileBody);
            }
            return builder.build();
        }

    }

    @Override
    protected RequestBody wraRequestBody(RequestBody requestBody, Callback callback) {
        if (callback == null){
            return requestBody;
        }
        CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody, new CountingRequestBody.Listener() {
            @Override
            public void onRequestProgress(long byteWritten, long contentLength) {

            }
        });
        return countingRequestBody;
    }

    @Override
    protected Request buildRequest(RequestBody requestBody) {
        return builder.post(requestBody).build();
    }


    private String guessMimeType(String path){
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentTypeFor = null;

            try {
                contentTypeFor = fileNameMap.getContentTypeFor(URLEncoder.encode(path, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            if (contentTypeFor == null){
                contentTypeFor = "application/octet-stream";
            }

            return contentTypeFor;

    }

    private void addParams(MultipartBody.Builder builder){
        if (params != null && !params.isEmpty()){
            for (String key : params.keySet()){
                builder.addPart(Headers.of("Content-Disposition","form-data; name=\"" + key + "\""),
                        RequestBody.create(null,params.get(key)));

            }
        }
    }

    private void addParams(FormBody.Builder builder){
        if (params != null){
            for (String key:params.keySet()){
                builder.add(key,params.get(key));
            }
        }
    }
}
